package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.UserAdminInsertParam;
import com.tcm.entity.dto.param.UserAdminUpdateParam;
import com.tcm.entity.dto.param.UserInfoUpdateParam;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.UserListResult;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.service.UserService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/7/2 21:05
 */
@Slf4j
@RestController
@RequestMapping("/pc/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据Token获取用户信息接口，若account参数不为空，则响应指定用户信息
     * @param request http请求对象
     * @return 响应数据
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getUserInfo(HttpServletRequest request) {
        return R.success(userService.getUserInfo(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 修改用户自己的个人信息
     * @param userInfoUpdateParam 用户更新信息参数封装
     * @param request             http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyUserInfoSelf(@RequestBody @Valid UserInfoUpdateParam userInfoUpdateParam, HttpServletRequest request) {
        boolean res = false;
        try {
            res = userService.modifyUserInfoSelf(userInfoUpdateParam, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 修改个人密码
     * @param param   参数Map
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(value = "/pwd", method = RequestMethod.PUT)
    public R modifyUserPasswordSelf(@RequestBody Map<String, String> param, HttpServletRequest request) {
        String oldPassword = param.get("oldPassword");
        String newPassword = param.get("newPassword");
        if (!StringUtils.hasText(oldPassword) || !StringUtils.hasText(newPassword)) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = userService.modifyUserPasswordSelf(oldPassword, newPassword, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 新增管理员用户
     * @param adminUpdateParam 管理员用户更新参数
     * @param request          http请求对象
     * @return 响应结果
     */
    @RequestMapping(value = "/admin", method = RequestMethod.POST)
    public R addAdminUser(@RequestBody @Valid UserAdminInsertParam adminUpdateParam, HttpServletRequest request) {
        boolean res = false;
        try {
            res = userService.addAdminUser(adminUpdateParam, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        return res ? R.success() : R.fail(102, "用户已存在");
    }

    /**
     * 分页获取管理员用户列表
     * @param page     当前页号
     * @param pageSize 页面大小
     * @param account  查询账号
     * @param name     查询名称
     * @return 响应结果
     */
    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public R getAdminUserListByPage(Integer page, Integer pageSize, String account, String name) {
        if (page == null || pageSize == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        PageR<UserListResult> adminUserListByPage = userService.getAdminUserListByPage(page, pageSize, account, name);
        return R.success(adminUserListByPage);
    }

    /**
     * 修改管理员用户信息
     * @param adminUpdateParam 管理员用户信息修改封装类
     * @param request          http请求对象
     * @return 响应对象
     */
    @RequestMapping(value = "/admin", method = RequestMethod.PUT)
    public R modifyAdminUserInfo(@RequestBody @Valid UserAdminUpdateParam adminUpdateParam, HttpServletRequest request) {
        boolean res = userService.modifyAdminUserInfo(adminUpdateParam, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_BLANK);
    }

    /**
     * 批量删除管理员用户
     * @param ids     用户ID列表
     * @param request http请求
     * @return 响应结果
     */
    @RequestMapping(value = "/admin", method = RequestMethod.DELETE)
    public R removeAdminUser(@RequestParam("ids") List<String> ids, HttpServletRequest request) {
        if (ids.isEmpty()) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = false;
        try {
            res = userService.removeAdminUser(ids, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(102, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 用户管理，分页获取用户列表
     * @param page     当前页码
     * @param pageSize 页面大小
     * @param account  查询账号，可选
     * @param name     查询姓名，可选
     * @param type     用户类型，可选
     * @return 响应结果
     */
    @RequestMapping(value = "/manage", method = RequestMethod.GET)
    public R getUserListByPage(Integer page, Integer pageSize, String account, String name, Integer type) {
        if (page == null || pageSize == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        PageR<UserListResult> userListByPage = userService.getUserListByPage(page, pageSize, account, name, type);
        return R.success(userListByPage);
    }

    /**
     * 用户管理，重置用户密码
     * @param param   参数封装
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(value = "/manage", method = RequestMethod.PUT)
    public R resetUserPassword(@RequestBody Map<String, Long> param, HttpServletRequest request) {
        String pwd = userService.resetUserPassword(param.get("id"), AccessTokenUtil.getUserId(request));
        return pwd != null ? R.success(pwd) : R.fail(ResponseEnum.HANDLE_FAIL);
    }

}
